package database.query;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import database.PasswdDatabaseManager;
import database.rescue.GetBlogDatabase;
import database.rescue.GetUserDatabase;

import user.User;
import user.UserIF;
import user.blog.Blog;
import user.blog.BlogIF;
import user.blog.post.Post;
import user.blog.post.comments.Comment;
import user.blog.post.comments.ManageComments;
import user.profile.Profile;
import user.profile.Flags;

/**
 * 
 * @author diego
 *
 */
public class Query {
	
	
	
	public static List<Profile> findProfiles(Flags flag, String match) throws FileNotFoundException, Exception {
		PasswdDatabaseManager pdm = PasswdDatabaseManager.getInstance();
		Properties prop = pdm.getPasswd();
		Iterator<String> itLogins = prop.stringPropertyNames().iterator();
				
		List<Profile>profiles = new ArrayList<Profile>();
		
		while(itLogins.hasNext()){
			
			String tempLogin = itLogins.next();
			GetUserDatabase gud = new GetUserDatabase(tempLogin);
			String type = gud.getProfile().getPersonalProperty(flag);//simplificar o codigo
			
						
			if (type.toLowerCase().contains(match.toLowerCase())) 
				profiles.add(gud.getProfile());
			
		}
		return profiles;
	}
	
	/**
	 * Acha blogs a partir de uma palavra
	 * 
	 * @param flag atributo a qual quer procurar um blog [titulo, descricao, idSession(id do usuario)]
	 * @param match palavra a qual esta procurando no blog
	 * @return um alista de blogs
	 * @throws FileNotFoundException
	 * @throws Exception
	 */
	public static List<BlogIF> findBlogs(String flag, String match) throws FileNotFoundException, Exception {
				
		List<UserIF>users;
		List<BlogIF>blogs = new ArrayList<BlogIF>();
		
		users = GetUserDatabase.getallUsers();
		
		Iterator<UserIF> it = users.iterator();
		
		while(it.hasNext()){
			
			
			if(flag.equals("*")) //retorna todos os blogs
				blogs.addAll(it.next().getBlogs());
			else{

				for(BlogIF blog :it.next().getBlogs()){

					if(blog.getProperties().getProperty(flag).contains(match))
						blogs.add(blog);
				}
			}
		}
		
		
		return blogs;
	}
	
	/**
	 * Acha posts
	 * 
	 * @param flag atributo a qual quer procurar um post [titulo, texto, blogID]
	 * @param match palavra a qual esta procurando no post
	 * @return uma lista de posts
	 * @throws FileNotFoundException
	 * @throws Exception
	 */
	public static List<Post> findPosts(String flag, String match) throws FileNotFoundException, Exception {
		
		
		List<Post>posts = new ArrayList<Post>();
			
		for(BlogIF blog :findBlogs("*","*")){


			if(flag.equals("*"))//retorna todos os posts
				posts.addAll(blog.getAllPosts());
			
			else{
				for(Post post: blog.getAllPosts()){
					
					System.out.println("blogID "+match+" PostBlogID "+post.getBlogId());

					if(flag.equals("text") && post.getText().contains(match))
						posts.add(post);
					else if(flag.equals("titulo") && post.getText().contains(match))
						posts.add(post);
					else if(flag.equals("blogID") && post.getBlogId().equals(match))
						posts.add(post);
				}
			}
		}
		
		
		
		return posts;
	}

	
	/**
	 * Acha usuario
	 * 
	 * @param flag atributo a qual quer procurar um usuario [login, sessionID]
	 * @param match palavra a qual esta procurando no usuario
	 * @return um usuario
	 * @throws FileNotFoundException
	 * @throws Exception
	 */

	public static UserIF findUser(String flag, String match){
		
		List<UserIF> users ;
		try {
			
			users = GetUserDatabase.getallUsers();
			for(UserIF user: users){

				if(flag.equals("login") && user.getLogin().equals(match))
					return user;
				
				else if(flag.equals("sessionID") && user.getSessionID().equals(match))
					return user;
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		

		
		return null;
	}
	

	
	/*public static void main(String args[]){
		try {
		    
			Profile profile = new Profile("yusuke", "urameshi", "Yusuke", "Yusuke@hakusho.jp", "Masculino",
					"19/08/1998", "Tokyio-Japao", "Luta","sou um dedetive", "lutas", "japonesa", "manga");
			
			User user = new User("yusuke", "urameshi", profile);			
				
			user.createBlog("1003", "title", "description");
			
			user.save();
			
			System.out.println(Query.findBlogs("titulo","ti").toString());
			
		
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/
	
}

